package com.weeego.util.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.sql.weeego.bean.base.kaopuIncludeBlog;
import com.sun.syndication.feed.synd.SyndCategory;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEnclosure;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import com.weeego.util.string.Md5;

public class Rss {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		// System.out.print(getOneHtml("http://sunnie411.popo.blog.163.com/rss/",
		// "GBK"));

		// parseRss();

	}

	/**
	 * 读取一个url的内容，并返回字符串。
	 * 
	 * @param htmlurl
	 * @return
	 * @throws IOException
	 */
	public static String getOneHtml(String htmlurl, String bianma)
			throws IOException {
		URL url;
		String temp;
		final StringBuffer sb = new StringBuffer();
		try {
			url = new URL(htmlurl);
			final BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream(), bianma));
			while ((temp = in.readLine()) != null) {
				sb.append(temp);
			}
			in.close();
		} catch (final MalformedURLException me) {
			// System.out.println("the url is error");
			me.getMessage();
			throw me;
		} catch (final IOException e) {
			e.printStackTrace();
			throw e;
		}
		return sb.toString();
	}

	/**
	 * 读取一个url的内容，并返回字符串。
	 * 
	 * @param htmlurl
	 * @return
	 * @throws IOException
	 */
	public static String getOneHtml(String htmlurl) throws IOException {
		URL url;
		String temp;
		final StringBuffer sb = new StringBuffer();
		try {
			url = new URL(htmlurl);
			final BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream(), "UTF-8"));
			while ((temp = in.readLine()) != null) {
				sb.append(temp);
			}
			in.close();
		} catch (final MalformedURLException me) {
			// System.out.println("the url is error");
			me.getMessage();
			throw me;
		} catch (final IOException e) {
			e.printStackTrace();
			throw e;
		}
		return sb.toString();
	}

	/**
	 * 163 :http://sunnie411.popo.blog.163.com/rss/
	 * http://sunnie411.popo.blog.163.com
	 * 
	 * QQ: http://feeds.qzone.qq.com/cgi-bin/cgi_rss_out?uin=34762749
	 * 
	 * 
	 * sina :http://blog.sina.com.cn/rss/zuaa.xml http://blog.sina.com.cn/zuaa
	 * 
	 * 
	 * blogger :http://xu-ping.blogspot.com/rss.xml
	 * 
	 * 
	 * 
	 */

	/**
	 * 读取rss内容，用到rome
	 */
	public static void parseRss() {
		String rss = "http://feeds.qzone.qq.com/cgi-bin/cgi_rss_out?uin=19810706";
		try {
			URL url = new URL(rss);
			// 读取Rss源
			XmlReader reader = new XmlReader(url);
			System.out.println("Rss源的编码格式为：" + reader.getEncoding());
			SyndFeedInput input = new SyndFeedInput();

			// 得到SyndFeed对象，即得到Rss源里的所有信息
			SyndFeed feed = input.build(reader);

			// 得到Rss新闻中子项列表
			List entries = feed.getEntries();
			// 循环得到每个子项信息
			for (int i = 0; i < entries.size(); i++) {
				SyndEntry entry = (SyndEntry) entries.get(i);
				// 标题、连接地址、标题简介、时间是一个Rss源项最基本的组成部分
				System.out.println("标题：" + entry.getTitle());
				System.out.println("连接地址：" + entry.getLink());
				SyndContent description = entry.getDescription();
				System.out.println("标题简介：" + description.getValue());
				System.out.println("发布时间：" + entry.getPublishedDate());
				// 以下是Rss源可先的几个部分
				System.out.println("标题的作者：" + entry.getAuthor());
				// 此标题所属的范畴
				List categoryList = entry.getCategories();
				if (categoryList != null) {
					for (int m = 0; m < categoryList.size(); m++) {
						SyndCategory category = (SyndCategory) categoryList
								.get(m);
						System.out.println("此标题所属的范畴：" + category.getName());
					}
				}
				// 得到流媒体播放文件的信息列表
				List enclosureList = entry.getEnclosures();
				if (enclosureList != null) {
					for (int n = 0; n < enclosureList.size(); n++) {
						SyndEnclosure enclosure = (SyndEnclosure) enclosureList
								.get(n);
						System.out.println("流媒体播放文件：" + entry.getEnclosures());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 读取rss内容，用到rome
	 * 
	 * @throws FeedException
	 * @throws IllegalArgumentException
	 * @throws IOException
	 */
	public static List parseRss(String rss) throws IllegalArgumentException,
			FeedException, IOException {
		List _rs = new ArrayList();
		URL url;
		url = new URL(rss);
		// 读取Rss源
		XmlReader reader = new XmlReader(url);
		// System.out.println("Rss源的编码格式为：" + reader.getEncoding());
		SyndFeedInput input = new SyndFeedInput();
		// 得到SyndFeed对象，即得到Rss源里的所有信息
		SyndFeed feed = input.build(reader);
		// 得到Rss新闻中子项列表
		List entries = feed.getEntries();
		// 循环得到每个子项信息

		for (int i = 0; i < entries.size(); i++) {

			kaopuIncludeBlog _kaopuIncludeBlog = new kaopuIncludeBlog();
			SyndEntry entry = (SyndEntry) entries.get(i);
			// 标题、连接地址、标题简介、时间是一个Rss源项最基本的组成部分
			_kaopuIncludeBlog.setTitle(entry.getTitle());
			_kaopuIncludeBlog.setLink(entry.getLink());
			SyndContent description = entry.getDescription();
			_kaopuIncludeBlog.setInfo(description.getValue());
			_kaopuIncludeBlog.setAddtime(entry.getPublishedDate());
			_kaopuIncludeBlog.setAuthor(entry.getAuthor());
			_kaopuIncludeBlog.setThekey(Md5.MD5(rss + entry.getTitle()
					+ entry.getPublishedDate().toString()));
			_kaopuIncludeBlog.setComeform(rss);
			_rs.add(_kaopuIncludeBlog);

		}
		return _rs;
	}
}
